package com.qingcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.qingcheng.dao.CategoryMapper;
import com.qingcheng.dao.TbCategoryMapper;
import com.qingcheng.domain.CategoryDto;
import com.qingcheng.pojo.goods.TbCategory;
import com.qingcheng.pojo.goods.TbSpu;
import com.qingcheng.service.TbCategoryService;
import com.qingcheng.util.CacheKey;
import com.qingcheng.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
*
* @author Simon
* @date 2020/07/27
*/
@Service
public class TbCategoryServiceImpl implements TbCategoryService {

    @Resource
    private TbCategoryMapper tbCategoryMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Resource
    private RedisTemplate goodsRedisTemplate;


   /* @Override
    public void saveCategoryTreeToRedis() {
        Example example = new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isShow","1");
        example.setOrderByClause("seq");
        List<TbCategory> tbCategories = tbCategoryMapper.selectByExample(example);
        List<Map> categoryListByParentId = findCategoryListByParentId(tbCategories, 0);

        // 存入redis
        goodsRedisTemplate.boundValueOps(CacheKey.CATEGORY_TREE).set(categoryListByParentId);
    }

    @Override
    public List<Map> findCategoryTree() {
        return (List<Map>)goodsRedisTemplate.boundValueOps(CacheKey.CATEGORY_TREE).get();
    }*/

    @Override
    public List<Map> queryAll() {
        List<TbCategory> tbCategory = tbCategoryMapper.selectAll();
        List<Map> categoryListByParentId = findCategoryListByParentId(tbCategory, 0);
        return categoryListByParentId;
    }

    private List<Map> findCategoryListByParentId(List<TbCategory> tbCategoryList,Integer
            parentId){
        List<Map> mapList=new ArrayList<>();
        for (TbCategory tbCategory:
             tbCategoryList) {
            if(parentId.equals(tbCategory.getParentId())) {
                Map map = new HashMap();
                map.put("value", tbCategory.getId());
                map.put("label", tbCategory.getName());
                List<Map> categoryListByParentId = findCategoryListByParentId(tbCategoryList, tbCategory.getId());
                if(categoryListByParentId.size()>0)
                    map.put("children", categoryListByParentId);
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public PageUtil<TbCategory> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbCategory> pages = (Page<TbCategory>) tbCategoryMapper.selectAll();
        PageUtil<TbCategory> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbCategory> pageQueryByAll(Integer pageNum, Integer pageSize, TbCategory tbCategory) {
        Example example=new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if(tbCategory != null){
            if(!StringUtils.isEmpty(tbCategory.getName())){
                criteria.andLike("name","%"+tbCategory.getName()+"%");
            }
            if(!StringUtils.isEmpty(tbCategory.getSeq())){
               criteria.andEqualTo("seq",tbCategory.getSeq());
            }
            if(!StringUtils.isEmpty(tbCategory.getParentId()))
                criteria.andEqualTo("parentId",tbCategory.getParentId());
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbCategory> pages = (Page<TbCategory>) tbCategoryMapper.selectByExample(example);
        PageUtil<TbCategory> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }

    @Override
    public PageUtil<CategoryDto> queryCategoryList(Integer pageNum, Integer pageSize, TbCategory tbCategory) {
        PageUtil pageUtil = new PageUtil();
        pageUtil.setRows(tbCategoryMapper.PageQueryList(pageNum,pageSize,tbCategory));
        pageUtil.setTotal(tbCategoryMapper.UniteCount(tbCategory));
        return pageUtil;
    }


    @Override
    public boolean saveTbCategory(TbCategory tbCategory) {
        int insert = tbCategoryMapper.insert(tbCategory);
        // saveCategoryTreeToRedis() // 修改数据重新进行缓存预热
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbCategory(TbCategory tbCategory) {
        int i = tbCategoryMapper.updateByPrimaryKeySelective(tbCategory);
        // saveCategoryTreeToRedis()
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbCategoryMapper.deleteByPrimaryKey(id);
        // saveCategoryTreeToRedis()
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbCategory findById(Integer id) {
        TbCategory tbCategory = tbCategoryMapper.selectByPrimaryKey(id);
        return tbCategory;
    }

    @Override
    public Integer C3IdQueryTem(Integer c3Id) {
        TbCategory tbCategory = tbCategoryMapper.selectByPrimaryKey(c3Id);
        if (tbCategory != null) {
            return tbCategory.getTemplateId();
        }
        return 0;
    }

    // 商品首页菜单列表
    @Override
    public List<Map> findCategoryTree() {
        return (List<Map>) goodsRedisTemplate.boundValueOps(CacheKey.CATEGORY_TREE).get();
    }


    // redis缓存查询
    @Override
    public void saveCategoryTreeRedis() {
        // 查询商品分类导航
        Example example=new Example(TbCategory.class);
        Example.Criteria cr=example.createCriteria();
        cr.andEqualTo("isShow","1");
        example.setOrderByClause("seq");
        List<TbCategory> map=tbCategoryMapper.selectByExample(example);
        List<Map> byParentId3 = findByParentId3(map, 0);
        goodsRedisTemplate.boundValueOps(CacheKey.CATEGORY_TREE).set(byParentId3);
    }

    // 今日推荐
    @Override
    public List<Map> Today() {
        return categoryMapper.Today();
    }


    // 根据商品的分类随机查询出推荐的手机
    @Override
    public List<Map> recommend(Integer category3) {
        List<Map>  son=categoryMapper.RandomRecommend(category3);
        return son;
    }

    // 商品详情表的推荐相关列表
    @Override
    public List<TbCategory> lists(String id) {
        List<TbCategory>  son=categoryMapper.lists(id);
        return son;
    }

    //  首页菜单家用电器显示
    @Override
    public HashMap<Map,List<TbSpu>> appliances(String parent_id) {
        System.out.println("进入");
        // 查询前七天的热门电器
        LocalDate date1=LocalDate.now().minusDays(7);
        LocalDate date2= LocalDate.now();
        List<TbSpu> spus = null;

        CategoryMapper categoryMapper = null;
        List<TbCategory> categories=categoryMapper.appliances(parent_id,date1,date2);
        if(categories.size()==0){
            categories = categoryMapper.Nullappliances(parent_id);
        }
        HashMap map=new HashMap();
        for (TbCategory category : categories) {
            spus = categoryMapper.NullHotGoods(category.getId());
            map.put(category,spus);
        }
        return map;
    }

    private List<Map> findByParentId3(List<TbCategory> map, int i) {
        List<Map> maps=new ArrayList<Map>();
        for(TbCategory category:map){
            if(category.getParentId().equals(i)){
                Map map1=new HashMap();
                map1.put("id",category.getId());
                map1.put("name",category.getName());
                map1.put("menus",findByParentId3(map,category.getId()));
                maps.add(map1);
            }
        }
        return maps;
    }

}
